package com.wpw.web.work.scanjs;

import org.apache.commons.lang.StringUtils;

import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

/**
 * @Description:
 * @ClassName: AlloctScanJs
 * @CreateDate: 2020-06-16 16:13
 * @author: wupengwei
 */
public class AlloctScanJs {

    /*var snArray = [
            "C02Q30XQFY0T","C02Q46PXFY0T","C02PV0H4FY0T","C02Q4APGFY0T","C02PV0K4FY0T","C02Q46UNFY0T","1WK7N42","8CG8522502","P901B0NC","P901B0L9","MP1ADKMR","MP1AC6DF","MP1AC6XX","MP1AC7N8","MP1AC93G","MP1AC846","MP1AC7HF","MP1AC4G9","MP1ACAR8","MP1A9DP7","MP1AC7GX","15KDBT2","15PDBT2","15Q9BT2","15HFBT2","15P9BT2","15DGBT2","15NDBT2","DH40JD2","1MLZCF2","H0WMFD2","5KVX7C2","24DFBD2","26KMBD2","GTL4GD2","FW234D2","24WPBD2","CT7ZND2","4QNXYC2","GF1S7C2","220HBD2","H1LGFD2","5WL4BC2","HS4NBD2","6CZTYC2","6DRRYC2","1ZXPBD2","1L20DF2","1LD3DF2","HRXJBD2","HS5MBD2","5WK2BC2","5WLZ9C2","3F804D2","1KY5DF2","82WFBD2","GSZ8GD2","HC8CTF2","6PSTXD2","FT28DF2","FT37DF2","FL434D2","6H9GFD2","6FW0GD2","GTV0GD2","1L53DF2","H1QNFD2","82PFBD2","H1LNFD2","H7GZKF2","H1BNFD2","6FT3GD2","6G57GD2","6HHHFD2","FSQJHG2","3NYDVG2","J18LHG2","77FLZH2","77QPZH2","77VJZH2","747RZH2","73XJZH2","17F9RH2","8DX49K2","B4HJDK2","1HWY1K2","960G5K2","970N5K2","BTDSVL2","BTSYVL2","35Z7KM2","HP41YL2","6DLC7J2","C2WJTP2","C4FFJK2","DGN9XJ2","J0Y1KL2","CX54YK2","D813GM2","2HG6YM2","5R6WLP2","HP94YL2","D8C4GM2","CKT5HP2","HQSWBP2","HQFXBP2","JTS0DF2","JTSJZH2","JTSHFD2","JTS3S62","JTS1JD2","5P9LMM2","5PYFMM2","1KT5ZQ2","1KX5ZQ2","1L06ZQ2","BDP88Q2","BDS48Q2","BDVC8Q2","BDX68Q2","BDY88Q2","D74VGQ2","BLF68Q2","BLQ48Q2","BLFB8Q2","BLL88Q2","8D3D4W2","7653B92","HCV7Q52","54BHS72","6CR650676K","6CR6506745","6CR631619V","6CR6383QD4","6CR7082WKB","6CR7082WJT","6CR7082WK1","6CR6382LN1","6CR6415XCL","6CR710499P","6CR710499V","6CR7170RPS","6CR7127KMX","4CV8148JTD","4CV8116QP8","4CV8116QPG","4CV81386V7","4CV8158KFR","4CE7332BTH","1CZ90100NK"
            ];
    var size = snArray.length;
for (var i = 0; i<size; i++) {
        var code = snArray[i];
        var idx = i+1;
        $("#scanAddInTable_productCode_" + idx).val(code);
        allot_scan_add.productCodeChange(document.getElementById("scanAddInTable_productCode_" + idx));
        allot_scan_add.productCodeKeyDown(document.getElementById("scanAddInTable_productCode_" + idx));
        $("#scanAddInTrToTable").click();
    }*/

    private static Integer snSize = 3;

    private static StringBuilder tail = new StringBuilder("var size = snArray.length;")
            .append("for (var i = 0; i<size; i++) {")
            .append("var code = snArray[i];")
            .append("var idx = i+1;")
            .append("$(\"#scanAddInTable_productCode_\" + idx).val(code);")
            .append("allot_scan_add.productCodeChange(document.getElementById(\"scanAddInTable_productCode_\" + idx));")
            .append("allot_scan_add.productCodeKeyDown(document.getElementById(\"scanAddInTable_productCode_\" + idx));")
            .append("$(\"#scanAddInTrToTable\").click();")
            .append("}");

    public static void main(String[] args) throws IOException {

        Map<String, List<String>> warehouseT2SNMap = getDataFromTxt();



        //构造中间部分
        int size = warehouseT2SNMap.size();
        System.out.println("一共" + size + "个仓库");

        String jsFile = "/Users/wupengwei/Desktop/改装调拨/调拨sn.js";
        Path jsPath = Paths.get(jsFile);
        BufferedWriter writer = Files.newBufferedWriter(jsPath, StandardCharsets.UTF_8);

        for (String warehouse : warehouseT2SNMap.keySet()) {

            String zhuShi = "/**    " + warehouse + "   **/";

            List<String> snList = warehouseT2SNMap.get(warehouse);
            List<String> cutSnList = null;
            for (int i = 0; i < snList.size(); i++) {
                String sn = snList.get(i);
                if (i % snSize == 0) {
                    if (cutSnList != null) {
                        writeCutSnlist(writer, cutSnList, zhuShi);
                    }
                    cutSnList = new ArrayList<>();
                    cutSnList.add(sn);
                } else {
                    cutSnList.add(sn);
                }
            }

            if (cutSnList != null && cutSnList.size() > 0) {
                writeCutSnlist(writer, cutSnList, zhuShi);
            }

        }
        writer.flush();
}

    private static void writeCutSnlist(BufferedWriter writer, List<String> cutSnList, String zhuShi) throws IOException {
        writer.newLine();
        writer.write(zhuShi);
        writer.newLine();
        writer.write("var snArray = [");

        for (int j = 0; j < cutSnList.size(); j++) {
            String cutSn = cutSnList.get(j);
            if (j == cutSnList.size() - 1) {
                writer.write("\""+ cutSn +"\"");
            } else {
                writer.write("\""+ cutSn +"\",");
            }
        }
        writer.write("];");

        writer.write(tail.toString());
    }


    private static Map<String, List<String>> getDataFromTxt() throws IOException {
        String filePath = "/Users/wupengwei/Desktop/改装调拨/调拨sn.txt";
        Path path = Paths.get(filePath);
        Map<String, List<String>> warehouseT2SNMap = new HashMap<>();
        List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
        int totalSize = lines.size();
        System.out.println("共" + totalSize + "条数据");

        Map<String, String> skuT2SkuMap = new HashMap<>();

        for (int i = 0; i < lines.size(); i++) {

            String line = lines.get(i);
            String[] split = line.split("\t");
            String warehouse = StringUtils.deleteWhitespace(split[0]);
            String sn = split[1];

            List<String> snList = warehouseT2SNMap.get(warehouse);
            if (snList == null) {
                snList = new ArrayList<>();
                warehouseT2SNMap.put(warehouse, snList);
            }
            snList.add(sn);
        }

        return warehouseT2SNMap;
    }
}